package com.ssy.lingxi.system.service.impl;

import com.ssy.lingxi.common.constant.basic.EnableDisableStatus;
import com.ssy.lingxi.common.model.vo.PageVO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.manage.api.feign.paas.OnlineServicePASSControllerFeign;
import com.ssy.lingxi.platform.manage.api.model.dto.onlineservice.EnableStatusReq;
import com.ssy.lingxi.platform.manage.api.model.dto.onlineservice.IdReq;
import com.ssy.lingxi.platform.manage.api.model.dto.onlineservice.OnlineServiceAddReq;
import com.ssy.lingxi.platform.manage.api.model.dto.onlineservice.OnlineServiceUpdateReq;
import com.ssy.lingxi.system.common.CommonBooleanEnum;
import com.ssy.lingxi.system.dao.OnlineServiceRepository;
import com.ssy.lingxi.system.entity.OnlineService;
import com.ssy.lingxi.system.model.request.IdVO;
import com.ssy.lingxi.system.model.response.OnlineServiceDetailRes;
import com.ssy.lingxi.system.model.response.OnlineServicePageRes;
import com.ssy.lingxi.system.service.IOnlineServiceService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * 基础设置-在线客服实现类
 * @author wyx
 * @version 2.0.0
 * @date 2021-11-16
 */
@Service
public class OnlineServiceServiceImpl implements IOnlineServiceService {
    @Resource
    private OnlineServiceRepository onlineServiceRepository;
    @Resource
    private OnlineServicePASSControllerFeign onlineServicePASSControllerFeign;


    /**
     * 新增客服
     * @param req 请求参数
     * @return 操作结果R
     */
    @Override
    public Wrapper<Void> add(OnlineServiceAddReq req) {
        //调用平台后台服务新增操作
        Wrapper<Void> result = onlineServicePASSControllerFeign.add(req);
        if (result.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(result.getCode(), result.getMessage());
        }

        OnlineService onlineService = new OnlineService();
        onlineService.setType(req.getType());
        onlineService.setParamConfigList(req.getParamConfigList());
        onlineService.setPlatformName(req.getPlatformName());
        onlineService.setStatus(CommonBooleanEnum.NO.getCode());
        onlineServiceRepository.save(onlineService);
        return Wrapper.success();
    }

    /**
     * 修改客服
     * @param req 请求参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> update(OnlineServiceUpdateReq req) {
        OnlineService onlineService = onlineServiceRepository.findById(req.getId()).orElse(null);
        if (onlineService == null) {
            return Wrapper.fail(ResponseCode.SYSTEM_RECORDS_DON_T_EXIST);
        }
        //调用平台后台服务更新操作
        Wrapper<Void> result = onlineServicePASSControllerFeign.update(req);
        if (result.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(result.getCode(), result.getMessage());
        }

        onlineService.setType(req.getType());
        onlineService.setParamConfigList(req.getParamConfigList());
        onlineService.setPlatformName(req.getPlatformName());
        onlineServiceRepository.save(onlineService);
        return Wrapper.success();
    }

    /**
     * 启用/停用
     * @param req 请求参数
     * @return 操作结果
     */
    @Override
    public Wrapper<Void> enable(EnableStatusReq req) {
        //检查是否有已使用的客服平台
        if (EnableDisableStatus.ENABLE.getCode().equals(req.getStatus())) {
            long count = onlineServiceRepository.countByStatus(CommonBooleanEnum.YES.getCode());
            if (count > 0) {
                return Wrapper.fail(ResponseCode.ONLINE_SERVICE_PLATFORM_ENABLE);
            }
        }
        OnlineService onlineService = onlineServiceRepository.findById(req.getId()).orElse(null);
        if (onlineService == null) {
            return Wrapper.fail(ResponseCode.SYSTEM_RECORDS_DON_T_EXIST);
        }
        //调用平台后台服务启用操作
        Wrapper<Void> result = onlineServicePASSControllerFeign.enable(req);
        if (result.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(result.getCode(), result.getMessage());
        }
        onlineService.setStatus(req.getStatus());
        onlineServiceRepository.save(onlineService);
        return Wrapper.success();
    }

    /**
     * 删除客服
     * @param req 请求参数
     * @return 返回结果
     */
    @Override
    public Wrapper<Void> delete(IdReq req) {
        OnlineService onlineService = onlineServiceRepository.findById(req.getId()).orElse(null);
        if (onlineService == null) {
            return Wrapper.fail(ResponseCode.SYSTEM_RECORDS_DON_T_EXIST);
        }
        if (EnableDisableStatus.ENABLE.getCode().equals(onlineService.getStatus())) {
            return Wrapper.fail(ResponseCode.MEMBER_START_NOT_ALLOW);
        }
        //调用平台后台服务删除操作
        Wrapper<Void> result = onlineServicePASSControllerFeign.delete(req);
        if (result.getCode() != ResponseCode.SUCCESS.getCode()) {
            return Wrapper.fail(result.getCode(), result.getMessage());
        }

        onlineServiceRepository.delete(onlineService);
        return Wrapper.success();
    }

    /**
     * 查看详情
     * @param idVO 请求参数
     * @return 返回结果
     */
    @Override
    public Wrapper<OnlineServiceDetailRes> detail(IdVO idVO) {
        OnlineService onlineService = onlineServiceRepository.findById(idVO.getId()).orElse(null);
        if (onlineService == null) {
            return Wrapper.fail(ResponseCode.SYSTEM_RECORDS_DON_T_EXIST);
        }
        return Wrapper.success(new OnlineServiceDetailRes(onlineService.getId(), onlineService.getPlatformName(), onlineService.getType(), onlineService.getParamConfigList()));
    }

    /**
     * 分页查看客服列表
     * @param pageVO 请求参数
     * @return 返回结果
     */
    @Override
    public Wrapper<PageData<OnlineServicePageRes>> page(PageVO pageVO) {
        Pageable page = PageRequest.of(pageVO.getCurrent() - 1, pageVO.getPageSize(), Sort.by("id"));
        Page<OnlineService> all = onlineServiceRepository.findAll(page);

        return Wrapper.success(new PageData<>(all.getTotalElements(), all.getContent().stream().map(onlineService ->
                new OnlineServicePageRes(onlineService.getId(), onlineService.getPlatformName(), onlineService.getType(), onlineService.getStatus())).collect(Collectors.toList())));
    }
}
